<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class K3WebService extends CI_Controller{
    public function index(){
        /*
        $this->dbConnectsql = $this->load->database("sqlserver",TRUE);
         //$res_dataone = $this->Data_model->get_single_data(array("UserId" => "B3DB39DC-16D1-4500-92BE-0755FB2F255D"),"aspnet_Membership");
         $query = $this->dbConnectsql->query("select t2.fbillno fbillno,t5.fname fusername,fentryselfp0253 fuserno,t3.fnumber fnumber,t3.fname fmattername,t3.fmodel fmodel,t3.fchartnumber fchartnumber, t3.fhelpcode fhelpcode,t1.fprice fprice,t4.fname gysname,t4.fphone gystel,t4.femail gysemail from poorderentry as t1
            inner join poorder as t2 on t1.finterid = t2.finterid 
            inner join t_icitem as t3 on t1.fitemid = t3.fitemid
            inner join t_supplier as t4 on t2.fsupplyid = t4.fitemid
            inner join  t_emp as t5 on t2.fempid = t5.fitemid
            where t1.FInterID = '1142' and t1.FEntryID = '1'");
         $res_dataone = $query->row_array();
         var_dump($res_dataone);
         * 
         */
    }
	
    public function getMatterInfo(){
        header("Access-Control-Allow-Origin:*");
        $this->dbConnectsql = $this->load->database("sqlserver",TRUE);
        $barCode = trim($_REQUEST["barCode"]);
		$barCode = substr($barCode,0,16);
        $query = $this->dbConnectsql->query("select t1.fqty Num,t2.fbillno OrderId,t5.fname Name,fentryselfp0253 userno,t3.fnumber MatterCode,t3.fname MatterName,t3.fmodel Model,t3.fchartnumber PicNo, t3.fhelpcode ProNo,t1.fprice Price,t4.fname SupplierName,t4.fphone SupplierTel,t4.femail SupplierEmail from poorderentry as t1
            inner join poorder as t2 on t1.finterid = t2.finterid 
            inner join t_icitem as t3 on t1.fitemid = t3.fitemid
            inner join t_supplier as t4 on t2.fsupplyid = t4.fitemid
            inner join  t_emp as t5 on t2.fempid = t5.fitemid
            where t1.fentryselfp0260 like '".$barCode."%' and ISNULL(t1.FENTRYSELFP0263,'') <> '1'");
        $resDate = $query->row_array();
        $json_date = array(
            'OrderId'         => $resDate['OrderId'],                 //订单
            'Name'            => $resDate['Name'],                    //采购员
            'userno'          => $resDate['userno'],                  //工号
            'MatterCode'      => $resDate['MatterCode'],              //物料代码
            'MatterName'      => $resDate['MatterName'],              //物料名称
            'Model'           => $resDate['Model'],                   //规格型号
            'PicNo'           => $resDate['PicNo'],                   //图号
            'ProNo'           => $resDate['ProNo'],                   //部品号
            'Price'           => $resDate['Price'],                   //单价
            'SupplierName'    => $resDate['SupplierName'],            //供应商名称
            'SupplierTel'     => $resDate['SupplierTel'],             //供应商电话
            'SupplierEmail'   => $resDate['SupplierEmail'],            //供应商邮箱
            'Num'             => $resDate["Num"],            //数量
            'errmsg'          => ""
        );
        if(empty($resDate['OrderId'])){
            $json_date['errmsg'] = "您选择的条码号已经生成过品质异常单或者该条码不存在，请检查！";
        }
        echo json_encode($json_date);
    }
    public function updateMatterStatus(){
        header("Access-Control-Allow-Origin:*");
        $barCode = trim($_REQUEST["barCode"]);
        $barCode = substr($barCode, 0,16);
        $status  = trim($_REQUEST["status"]);
		//echo $status;
        if($status == "退货"){
            $status = 40481;
        }else if($status == "换货"){
            $status = 40482;
        }else if($status == "特采"){
            $status = 40483;
        }else if($status == "换货完成"){
            $status = 40484;
        }else if($status == "特采完成"){
            $status = 40485;
        }else if($status == "检验合格"){
            $status = 40486;
        } 
        $this->dbConnectsql = $this->load->database("sqlserver",TRUE);
        $query = $this->dbConnectsql->query("update icstockbillentry set FEntrySelfA0159 = '$status' where FEntrySelfA0161 like '$barCode%'");
        $json_date = array(
            "barCode" => $barCode,
            "status" => $status
        );
        echo json_encode($json_date);
    }
    public function updateorderstatus(){
        header("Access-Control-Allow-Origin:*");
        $barCode = trim($_REQUEST['barCode']);
		$barCode = substr($barCode,0,16);
        $this->dbConnectsql = $this->load->database("sqlserver",TRUE);
        $query = $this->dbConnectsql->query("update poorderentry set FENTRYSELFP0263 = '1' where FEntrySelfp0260 like '$barCode%'");
        $json_date = array(
            "barCode" => $barCode,
        );
        echo json_encode($json_date);
    }
    //品质检验单提交检验结果
    public function submitcheckresult(){
        header("Access-Control-Allow-Origin:*");
        $fbatchno = trim($_REQUEST['fbatchno']);          //工号
        $fnumber  = trim($_REQUEST['fnumber']);           //物料代码
        $fname    = trim($_REQUEST['fname']);             //物料名称
        $result   = trim($_REQUEST['result']);            //检验结果
        $reason   = trim($_REQUEST['reason']);            //原因
        //echo "insert into Quality (fbatchno,fnumber,fname,result,reason) values ('"+$fbatchno+"','"+$fnumber+"','"+$fname+"',"+$result+",'"+$reason+"');";
        $this->dbConnectsql = $this->load->database("sqlserver",TRUE);
        $query = $this->dbConnectsql->query("insert into Quality (fbatchno,fnumber,fname,result,reason) values ('".$fbatchno."','".$fnumber."','".$fname."',".$result.",'".$reason."');");
        
        $json_data  = array(
            "return" => "success"
        );
        echo json_encode($json_data);
    }
    /*
     * 供品质检验单调取条码数据
     */
    public function getMatterInfo_PZJYD(){
        header("Access-Control-Allow-Origin:*");
        $this->dbConnectsql = $this->load->database("sqlserver",TRUE);
        $barCode = trim($_REQUEST["barCode"]);
		$barCode = substr($barCode,0,16);
        $query = $this->dbConnectsql->query("select t1.fqty Num,t2.fbillno OrderId,t5.fname Name,fentryselfp0253 userno,t3.fnumber MatterCode,t3.fname MatterName,t3.fmodel Model,t3.fchartnumber PicNo, t3.fhelpcode ProNo,t1.fprice Price,t4.fname SupplierName,t4.fphone SupplierTel,t4.femail SupplierEmail from poorderentry as t1
            inner join poorder as t2 on t1.finterid = t2.finterid 
            inner join t_icitem as t3 on t1.fitemid = t3.fitemid
            inner join t_supplier as t4 on t2.fsupplyid = t4.fitemid
            inner join  t_emp as t5 on t2.fempid = t5.fitemid
            where t1.fentryselfp0260 like '".$barCode."%'");
        $resDate = $query->row_array();
        $json_date = array(
            'OrderId'         => $resDate['OrderId'],                 //订单
            'Name'            => $resDate['Name'],                    //采购员
            'userno'          => $resDate['userno'],                  //工号
            'MatterCode'      => $resDate['MatterCode'],              //物料代码
            'MatterName'      => $resDate['MatterName'],              //物料名称
            'Model'           => $resDate['Model'],                   //规格型号
            'PicNo'           => $resDate['PicNo'],                   //图号
            'ProNo'           => $resDate['ProNo'],                   //部品号
            'Price'           => $resDate['Price'],                   //单价
            'SupplierName'    => $resDate['SupplierName'],            //供应商名称
            'SupplierTel'     => $resDate['SupplierTel'],             //供应商电话
            'SupplierEmail'   => $resDate['SupplierEmail'],            //供应商邮箱
            'Num'             => $resDate["Num"],            //数量
            'errmsg'          => ""
        );
        if(empty($resDate['OrderId'])){
            $json_date['errmsg'] = "您选择的条码号已经生成过品质异常单或者该条码不存在，请检查！";
        }
        echo json_encode($json_date);
    }
    
}